/**
 *   @file 应用rem替代物理像素，支持糯米适配
 *   计算原理：当前屏幕宽与标准320的宽度比
 *   @author: Lori<wangccong@baidu.com>
 */
let doc = window.document;
let docEl = doc.documentElement;
let tid = null;

function setViewportByDevicePixelRatio(ratio) {
    let scale = 1 / ratio;
    let viewport = document.querySelector('meta[name=viewport]');
    let content = `width=device-width,initial-scale=${scale},
        maximum-scale=${scale}, minimum-scale=${scale}, user-scalable=no`;
    viewport.setAttribute('content', content);
}

function refreshRem() {
    // 处理1像素问题
    setViewportByDevicePixelRatio(window.devicePixelRatio);
    let width = docEl.getBoundingClientRect().width;
    let rem = width / 10;
    docEl.style.fontSize = rem + 'px';
    if (width >= 414) {
        docEl.setAttribute('data-fontscale', 2);
    }
    else {
        docEl.setAttribute('data-fontscale', 1);
    }
}
window.addEventListener('resize', () => {
    clearTimeout(tid);
    tid = setTimeout(refreshRem, 300);
}, false);
window.addEventListener('pageshow', e => {
    if (e.persisted) {
        clearTimeout(tid);
        tid = setTimeout(refreshRem, 300);
    }
}, false);
doc.body.style.fontSize = '12px';
refreshRem();
